require "scripts/include/operative"

function InitLevel()
	levelEnded = false
	levelEndTime = 0
	playerId = dbGetPlayerId()
end

function UpdateExplosives()
	ForEachItem(function(item)
		if item.attributes:find("X", 1, true) ~= nil then
			if item.health <= 0.0 then
				local bodyId = item.bodyId
				x = b2GetBodyPositionX(bodyId)
				y = b2GetBodyPositionY(bodyId)
				dbMakeExplosion(x, y, 500000000.0, 100.0)
				dbDeleteItem(item.itemId)
			end
		end
	end)
end

function UpdateLevel(deltaTime)
	UpdateExplosives()
	
	if levelEnded == false then
		if items(playerId).health <= 0.0 then
			levelCompleted = false
			levelEnded = true
			levelEndTime = os.time()
		end
	end
	
	local deltaTime = os.difftime(os.time(), levelEndTime)
		
	-- Return true if level has ended
	return levelEnded and (deltaTime >= 3) -- Wait 3 seconds before ending level
end

function DeinitLevel()
end
